<?php
/*
 * 取用户的在线状态
 * @param array $user = array('user1','user2');
 * @ret array array(
 *    'user1' => 1 //在线
 *    'user2' => 0 //不在线
 *    );
 */
function fo_get_online_users($users){
//取得用户昵称 最多可以传入100个用户名
	$user_count	= count($users);
	$tmp_array	= array();
	$ret_array	= array();
	$split_numb	= 100;	//每次取的条数
	$user_count	= count($users);
	$get_times	= ceil($user_count /$split_numb);	//分成几份
	for($i=0; $i<$get_times;$i++)
	{
		//取出当前的一段数据
		$tmp_array	= array_slice($users, $i*$split_numb, $split_numb);
		//取出昵称 并到返回的数组
		$ret_array	= array_merge($ret_array,online_batch_query($tmp_array));
	}
	$ret = array();
//$user_array	= array("alan" => "0100000000000000","admin" => "0100000000000000","angelindream" => "0100000000000000","babyziyi" => "0100000000000000","kly2066" => "0100000000000000","kohleifoh01" => "0100000000000000","lisa925" => "0100000000000000");
	foreach($ret_array as $key => $value){
		//$value 返回一传字节，$value{1}标识网站在线，$value{2}标识im在线，3标识ktv在线，4表示wap在线。
		//=2 隐身
		$ret[$key] = 0;
		//im  不是隐身和离线状态
		if($value{2} != 0 && $value{2} != 2){
			$ret[$key] = 1;
			//在线
		}
		//网站 不是离线和隐身状态
		//	else if($value{1} != 0 && $value{1} != 2)
		else if($value{1} != 0 && $value{1} != 2 && $value{2} !=2){
			$ret[$key] = 1;
		}
	}
	return $ret;
}

//status 1=online，0=offline，2=hidden
function online_query($user){	 
	$account= "account:" . $user ; //
	$comm = "X". sprintf("%04x",strlen($account)) . $account;
	 
	$serverInfo = UDPServerInfo($user) ;
	$rs = ConnectByUDP($user,$comm,$serverInfo);
	if(!$rs)
	{
		$rs = ConnectByUDP($user,$comm,$serverInfo);
	}
	$ret=explode("STATUS:",$rs);	
	$status=$ret[1];
	$size=strlen($status);
	for($i=0;$i<16-$size;$i++){
		 $status.='0';
	}
	return $status;
}
/*
	一次可传入N个元素的数组，元素为用户名
*/
function qo_batch_query($userArray){
	$array2d=array_chunk($userArray, 100);
	$ret_arr=array();
	for($i=0;$i<count($array2d);$i++){
		$tmp_arr=online_batch_query($array2d[$i]);
		$ret_arr=array_merge($ret_arr,$tmp_arr);
	}
	return $ret_arr;
}
/*
	一次最多可传入100个元素的数组，元素为用户名
*/
function online_batch_query($userArray){	
	if(count($userArray)>100){
		return false;
	}
	foreach($userArray as $user){
		$pear = hexdec(substr(md5($user),0,4)) % 2 ;
		if($pear==0)
			$hash_0[]=$user;
		else
			$hash_1[]=$user;
	}
	$ret_0=online_batch_query_hash($hash_0);
	$ret_1=online_batch_query_hash($hash_1);
	if($ret_0){
		foreach($ret_0 as $k=>$v){
			$ret[$k]=$v;
		}
	}
	if($ret_1){
		foreach($ret_1 as $k=>$v){
			$ret[$k]=$v;
		}
	}
	asort($ret);
	return $ret;
}

function online_batch_query_hash($userArray){	
	if(count($userArray)==0 || count($userArray)>100){
		return false;
	}
	foreach($userArray as $user){
		$tmp.=$user.",";
	}
	$tmp =substr($tmp ,0,-1);

	$comm = "Y". $tmp ;
	$serverInfo = UDPServerInfo($user) ;
	$rs = ConnectByUDP($user,$comm,$serverInfo);
	if(!$rs)
	{
		$rs = ConnectByUDP($user,$comm,$serverInfo);
	}
	$rs=substr($rs,1);
	$rs_arr=explode(",",$rs);
	$cnt=count($rs_arr);
	for($i=0;$i<$cnt;$i++){
		$temp=explode(":",$rs_arr[$i]);
		$status=$temp[1];
		$size=strlen($status);
		for($ii=0;$ii<16-$size;$ii++){
		 	$status.='0';
		}
		$ret_user[$temp[0]]=$status;
	}
	return $ret_user;
}


function ConnectByUDP($user,$comm,$serverInfo){
	if(empty($user)){
		return false;
	}
	$fp = fsockopen("udp://{$serverInfo["host"]}",$serverInfo["port"],$errno,$errstr,0.5);
	if ($fp === false) {
		return false;
	}
	stream_set_timeout($fp, 0, 100000);
	$result = fwrite($fp,$comm);
	if ($result === false){
		fclose($fp);
		return false;
	}
	$buff=fread($fp,10240);
	fclose($fp);
	return trim($buff);
}

function UDPServerInfo($user){
	$arrInfo = array(0 => array("host" => "online1.bierenshuo.com", "port" => "15061"),
					 1 => array("host" => "online2.bierenshuo.com", "port" => "15061") );
	$pear = hexdec(substr(md5($user),0,4)) % 2 ;
	return $arrInfo[$pear] ;
}
?>
